package com.definity.toolkit.ui.event;

import com.definity.toolkit.domain.Domain;
import com.definity.toolkit.ui.widget.InternalFrame;

public class DomainEventObserver implements Observer {

	private final Class<? extends Domain<?>> domain;
	private final Runnable runnable;

	public DomainEventObserver(Class<? extends Domain<?>> domain, final InternalFrame frame) {
		this.domain = domain;
		this.runnable = new Runnable() {
			@Override
			public void run() {
				frame.loadData();
			}
		};
	}

	public DomainEventObserver(Class<? extends Domain<?>> domain, Runnable runnable) {
		this.domain = domain;
		this.runnable = runnable;
	}
	
	@Override
	public void update(Event event) {
		if (event instanceof DomainEvent) {
			if (domain.isInstance(((DomainEvent<?>) event).getDomain()))
				runnable.run();
		}
	}

}
